import {watchEffect} from '../reactivity/index.js';
import {patch, mountElement} from './index.js';

export default function createApp(rootComponent) {
  return {
    mount(rootContainer) {
      // setup 返回一个对象，对象的属性分别是导出的响应式数据
      const context = rootComponent.setup();
      let isMounted = false;
      let preVnode;

      watchEffect(() => {
        if (!isMounted) {
          isMounted = true;
          const newVnode = rootComponent.render(context);
          mountElement(newVnode, rootContainer);
          preVnode = newVnode;
        } else {
          const newVnode = rootComponent.render(context);
          patch(preVnode, newVnode);
          preVnode = newVnode;
        }
      });
    }
  };
}
